package com.ymatou.app.rec.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.ShoppingCarGlProductBean;
import com.ymatou.app.rec.service.ShoppingCarGlProductCall;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.ShopCartMongoBean;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.database.sqlwapper.CSearchWapper;
import com.ymttest.database.sqlwapper.SearchWapper;

public class Ts_ShoppingCarGlProduct {
	private static ShoppingCarGlProductBean shoppingCarGLProductBean;
	private static ShoppingCarGlProductCall shoppingCarGLProductCall;
	private static ShoppingCartMongoWapper scw;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("购物车猜你喜欢推荐接口");
	}

	@Before
	public void caseUp() {
		shoppingCarGLProductBean = new ShoppingCarGlProductBean();
		shoppingCarGLProductCall = new ShoppingCarGlProductCall();
		try {
			scw = new ShoppingCartMongoWapper();
		} catch (Exception e) {
			Logger.setup("MongoDB初始化失败：" + e.getMessage(), -1);
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingCarGLProductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_001() {
		Logger.start(true,"购物车猜你喜欢推荐接口-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int userid = 4085;
			
			shoppingCarGLProductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,10");
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			JSONArray hits = shoppingCarGLProductCall.getHits();
			
			Logger.verifyEquals("OK", shoppingCarGLProductCall.getStatus(), "验证接口返回状态是否正常");
			Logger.verifyEquals("OK",shoppingCarGLProductCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(shoppingCarGLProductCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, shoppingCarGLProductCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, shoppingCarGLProductCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, shoppingCarGLProductCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(shoppingCarGLProductCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, shoppingCarGLProductCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_002() {
		Logger.start(true,"购物车猜你喜欢推荐接口-检查接口返回数据，确保不会重复 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int userid = 4085;
			
			shoppingCarGLProductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,10");
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			Logger.verifyEquals("OK", shoppingCarGLProductCall.getStatus(), "验证接口返回状态是否正常");
			JSONArray hitsAry = shoppingCarGLProductCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String sproductid = json.getString("sproductid");

				if (list.contains(sproductid) == true) {

					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id存在重复值" + sproductid);

				} else {
					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id不存在重复值" + sproductid);

					list.add(sproductid);

				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_003() {
		Logger.start(true,"购物车猜你喜欢推荐接口-验证searchInfo里的返回数据");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int userid = 4085;
			
			shoppingCarGLProductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,10");
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) shoppingCarGLProductCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_004() {
		Logger.start(true,"购物车猜你喜欢推荐接口-验证接口返回商品是否都为有效商品");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int userid = 4085;
			
			shoppingCarGLProductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,10");
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			Logger.verifyEquals("OK", shoppingCarGLProductCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= shoppingCarGLProductCall.getHits().length(); i++) {

				String info = shoppingCarGLProductCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int sourcesInt =  (Integer) infoAry.get("source");
				
				String productId = (String) infoAry.get("sproductid");
				
				
				List<Map> ProductLists = null;
				if (sourcesInt==1) {
					//返回M端数据库里的商品信息
					 ProductLists = new SearchWapper().getProductBySellerId(0,"","",productId);
				}else{
					//返回C端数据库里的商品信息
					 ProductLists = new CSearchWapper().getProductById(productId);
				}
				
				Logger.verifyEquals(1, ProductLists.size(), "检查第"+i+"条商品id为："+productId+"的商品是否满足能都搜索出来的要求");
				
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_005() {
		Logger.start(true,"购物车猜你喜欢推荐接口-购物车内有商品，且购物车商品能够匹配到同类目数量大于每页显示数的其他有效商品,验证推荐商品推荐是否为购物车商品类目子集");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 20;
			int uid = 5927529;
			//int uid = 20226434;
			
			shoppingCarGLProductBean.setInfo("userid:"+uid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,"+limit);
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			//连接mongo库查询用户购物车新增购物车的前100条商品信息
			List<Map> list = scw.selectShoppingInfo(uid, 100);
			//定义一个新数组存放购物车商品的productid
			List<String> CartIdList = new ArrayList<String>();
			
			for(int i=0;i<list.size();i++){
				String productid = list.get(i).get("productid").toString();
				CartIdList.add(productid);
			}
			
			//获取购物车商品对应的品牌和类目
			//因为不知道到底是c端还是m端商品，所以先去m端查，如果没有则去c端查，都没有则直接返回
			
			List<Map> ProductLists = null;
			//存放购物车商品的三级类目和品牌
			List thirdCategoryList = new ArrayList<String>();
			List iBrandIdList = new ArrayList<String>();
			
			for(int j=0;j<CartIdList.size();j++){
				// ProductLists = new SearchWapper().getProductBySellerId(0,"","",CartIdList.get(j));
				 ProductLists = new SearchWapper().selectProductInfo(CartIdList.get(j));
				 if(ProductLists.equals(null)){
					// ProductLists = new CSearchWapper().getProductById(CartIdList.get(j)); 
					ProductLists = new CSearchWapper().selectProductInfo(CartIdList.get(j));
					
				 }
				 
				 if(ProductLists.size()==0){
					 Logger.verifyNotNull(ProductLists, "未找到商品id为："+CartIdList.get(j)+"的商品");
				 }else{
					 int iBrandId = (Integer) ProductLists.get(0).get("iBrandId");
					 int iThirdCategoryId = (Integer) ProductLists.get(0).get("iThirdCategoryId");
					 thirdCategoryList.add(iThirdCategoryId);
					 iBrandIdList.add(iBrandId);
					 
				 }
				
			}
			
			//获取接口返回商品信息
			List<Map> HitProductInfoList = null;
			//存放接口返回的商品的三级类目和品牌
			List hitThirdCategoryList = new ArrayList<String>();
			List hitiBrandIdList = new ArrayList<String>();
			
			for (int y = 1; y <= shoppingCarGLProductCall.getHits().length(); y++) {

				String info = shoppingCarGLProductCall.getHits().get(y - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int sourcesInt =  (Integer) infoAry.get("source");
				
				String productId = (String) infoAry.get("sproductid");
				
				
				if (sourcesInt==1) {
					//返回M端数据库里的商品信息
					HitProductInfoList = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					HitProductInfoList = new CSearchWapper().selectProductInfo(productId);
				}
				
				if(HitProductInfoList.size()==0){
					 Logger.verifyNotNull(HitProductInfoList, "未找到商品id为："+productId+"的商品");
				 }else{
					 int iBrandId = (Integer) HitProductInfoList.get(0).get("iBrandId");
					 int iThirdCategoryId = (Integer) HitProductInfoList.get(0).get("iThirdCategoryId");
					 hitThirdCategoryList.add(iThirdCategoryId);
					 hitiBrandIdList.add(iBrandId);
					 
				 }
				
			}
			
			//接口返回商品类目是否包含在购物车商品类目中
			boolean isthirdCategory = thirdCategoryList.containsAll(hitThirdCategoryList);
			boolean isiBrandId = iBrandIdList.containsAll(hitiBrandIdList);
			
			Logger.verifyEquals(true, isthirdCategory, "验证接口推荐商品的类目是否是购物车商品类目");
			//Logger.verifyEquals(true, isiBrandId, "验证接口返回推荐商品的品牌是否是购物车商品品牌");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	//购物车中增加userid=5927529，productid为  iThirdCategoryId=1103的商品id，
	public void Tc_ShoppingCarGlProduct_006() {
		Logger.start(true,"购物车猜你喜欢推荐接口-购物车内有商品，且购物车商品能够匹配到同类目数量不足每页显示数，需要匹配同类品牌的其他有效商品,验证推荐商品推荐的类目和品牌是否包含购物车商品类目和品牌");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 15;
			int uid = 20226434;
			//int uid = 20226434;
			
			shoppingCarGLProductBean.setInfo("userid:"+uid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,"+limit);
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			//连接mongo库查询用户购物车新增购物车的前100条商品信息
			List<Map> list = scw.selectShoppingInfo(uid, 100);
			//定义一个新数组存放购物车商品的productid
			List<String> CartIdList = new ArrayList<String>();
			
			for(int i=0;i<list.size();i++){
				String productid = list.get(i).get("productid").toString();
				CartIdList.add(productid);
			}
			
			//获取购物车商品对应的品牌和类目
			//因为不知道到底是c端还是m端商品，所以先去m端查，如果没有则去c端查，都没有则直接返回
			
			List<Map> ProductLists = null;
			//存放购物车商品的三级类目和品牌
			List thirdCategoryList = new ArrayList<String>();
			List iBrandIdList = new ArrayList<String>();
			
			for(int j=0;j<CartIdList.size();j++){
				// ProductLists = new SearchWapper().getProductBySellerId(0,"","",CartIdList.get(j));
				 ProductLists = new SearchWapper().selectProductInfo(CartIdList.get(j));
				 if(ProductLists.equals(null)){
					// ProductLists = new CSearchWapper().getProductById(CartIdList.get(j)); 
					ProductLists = new CSearchWapper().selectProductInfo(CartIdList.get(j));
					
				 }
				 
				 if(ProductLists.size()==0){
					 Logger.verifyNotNull(ProductLists, "未找到商品id为："+CartIdList.get(j)+"的商品");
				 }else{
					 int iBrandId = (Integer) ProductLists.get(0).get("iBrandId");
					 int iThirdCategoryId = (Integer) ProductLists.get(0).get("iThirdCategoryId");
					 thirdCategoryList.add(iThirdCategoryId);
					 iBrandIdList.add(iBrandId);
					 
				 }
				
			}
			
			//获取接口返回商品信息
			List<Map> HitProductInfoList = null;
			//存放接口返回的商品的三级类目和品牌
			List hitThirdCategoryList = new ArrayList<String>();
			List hitiBrandIdList = new ArrayList<String>();
			
			for (int y = 1; y <= shoppingCarGLProductCall.getHits().length(); y++) {

				String info = shoppingCarGLProductCall.getHits().get(y - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int sourcesInt =  (Integer) infoAry.get("source");
				
				String productId = (String) infoAry.get("sproductid");
				
				
				if (sourcesInt==1) {
					//返回M端数据库里的商品信息
					HitProductInfoList = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					HitProductInfoList = new CSearchWapper().selectProductInfo(productId);
				}
				
				if(HitProductInfoList.size()==0){
					 Logger.verifyNotNull(HitProductInfoList, "未找到商品id为："+productId+"的商品");
				 }else{
					 int iBrandId = (Integer) HitProductInfoList.get(0).get("iBrandId");
					 int iThirdCategoryId = (Integer) HitProductInfoList.get(0).get("iThirdCategoryId");
					 hitThirdCategoryList.add(iThirdCategoryId);
					 hitiBrandIdList.add(iBrandId);
					 
				 }
				
			}
			
			//接口返回商品类目是否包含在购物车商品类目中
			boolean isthirdCategory = hitThirdCategoryList.containsAll(thirdCategoryList);
			boolean isiBrandId = hitiBrandIdList.containsAll(iBrandIdList);
			
			Logger.verifyEquals(true, isthirdCategory, "验证购物车商品类目是否在都包含在推荐商品的类目中");
			Logger.verifyEquals(true, isiBrandId, "验证购物车商品品牌是否都包含在推荐商品的品牌中");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_007() {
		Logger.start(true,"购物车猜你喜欢推荐接口-购物车内有商品，且购物车商品所对应类目和品牌对应的有效商品不足每页显示个时,验证推荐商品的商家id是否包含在购物车商品的商家id中");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 20;
			int uid = 20226434;
			//int uid = 20226434;
			
			shoppingCarGLProductBean.setInfo("userid:"+uid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,"+limit);
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			//连接mongo库查询用户购物车新增购物车的前100条商品信息
			List<Map> list = scw.selectShoppingInfo(uid, 100);
			//定义一个新数组存放购物车商品对应的sellerid
			List<Object> sellerIdList = new ArrayList<Object>();
			
			for(int i=0;i<list.size();i++){
				int sellerid = (Integer) list.get(i).get("sellerid");
				sellerIdList.add(sellerid);
			}
			
			
			//获取接口返回商品信息
			List<Map> HitProductInfoList = null;
			//存放接口返回的商品的商家id
			List iuserIdList = new ArrayList<String>();
			
			for (int y = 1; y <= shoppingCarGLProductCall.getHits().length(); y++) {

				String info = shoppingCarGLProductCall.getHits().get(y - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int sourcesInt =  (Integer) infoAry.get("source");
				
				String productId = (String) infoAry.get("sproductid");
				
				
				if (sourcesInt==1) {
					//返回M端数据库里的商品信息
					HitProductInfoList = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					HitProductInfoList = new CSearchWapper().selectProductInfo(productId);
				}
				
				if(HitProductInfoList.size()==0){
					 Logger.verifyNotNull(HitProductInfoList, "未找到商品id为："+productId+"的商品");
				 }else{
					 int iUserId = (Integer) HitProductInfoList.get(0).get("iUserId");
					 
					 iuserIdList.add(iUserId);
					
				 }
				
			}
			
			//取两者交集
			boolean isSellerId = sellerIdList.retainAll(iuserIdList);
			Logger.verifyEquals(true, isSellerId, "推荐商品的商家id与购物车商品的商家id有交集");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShoppingCarGlProduct_008() {
		Logger.start(true,"购物车猜你喜欢推荐接口-购物车无商品，且该用户无感兴趣数据，验证推荐商品是否按热度推荐");
		try {
			//设置参数
			String requestid = "1111111";
			String module = " shoppingcar_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			//该用户购物车无信息且无感兴趣数据
			int uid = 123455;
			//int uid = 20226434;
			
			shoppingCarGLProductBean.setInfo("userid:"+uid+",cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			shoppingCarGLProductBean.setAppkey("1");
			shoppingCarGLProductBean.setRequestid(requestid);
			shoppingCarGLProductBean.setModule(module);
			shoppingCarGLProductBean.setSubmodule(submodule);
			shoppingCarGLProductBean.setPlatform(platform);
			shoppingCarGLProductBean.setLimit("0,10");
			
			shoppingCarGLProductCall.setData(shoppingCarGLProductBean);
			shoppingCarGLProductCall.callService();
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}
